package com.ts.main.mini.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.main.mini.entity.ProductVariantEntity;
import com.ts.main.mini.exception.CommonException;
import com.ts.main.mini.mapper.ProductVariantMapper;
import com.ts.main.mini.vo.ProductVariantVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
* @author lk
* @description 针对表【t_product_variant】的数据库操作Service实现
* @createDate 2025-05-30 23:37:42
*/
@AllArgsConstructor
@Service
public class ProductVariantServiceImpl extends ServiceImpl<ProductVariantMapper, ProductVariantEntity>{

    public ProductVariantVo getProductCombination(Integer productId, String optionCombination) {
        return baseMapper.getProductCombination(productId,optionCombination);
    }

    public Boolean reduceStockQuantity(Integer variantId, Integer stockQuantity) {
        ProductVariantEntity variantEntity = getById(variantId);
        int quantity = variantEntity.getStock() - stockQuantity;
        if (quantity <= 0) {
            throw new CommonException("商品库存不足");
        }
        LambdaUpdateWrapper<ProductVariantEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ProductVariantEntity::getStock, quantity)
                .eq(ProductVariantEntity::getProductId, variantId);
        return update(updateWrapper);
    }

    public Boolean increaseStockQuantity(Integer variantId, Integer stockQuantity) {
        ProductVariantEntity variantEntity = getById(variantId);
        int quantity = variantEntity.getStock() + stockQuantity;
        LambdaUpdateWrapper<ProductVariantEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ProductVariantEntity::getStock, quantity)
                .eq(ProductVariantEntity::getVariantId, variantId);
        return update(updateWrapper);
    }
}




